package com.zhkj.txg.utils

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.zhkj.lib.base.BaseApplication
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.txg.R
import com.zhkj.txg.base.GlideApp
import com.zhkj.txg.utils.transform.CropCircleWithBorderTransformation
import com.zhkj.txg.utils.transform.RoundedCornersTransformation
import java.io.File

object ImageUtils {

    private var placeholderResId: Int = R.drawable.ic_loading
    private var errorResId: Int = R.drawable.ic_loading

    /**
     * 加载图片
     */
    fun loadImage(
        activity: Activity,
        imageView: ImageView,
        imgUrl: String,
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(activity).asDrawable().centerCrop().load(imgUrl)
            .placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 加载图片
     */
    fun loadImage(
        fragment: Fragment,
        imageView: ImageView,
        imgUrl: String,
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(fragment).asDrawable().centerCrop().load(imgUrl)
            .placeholder(placeholder).error(error).into(imageView)
    }


    /**
     * 加载图片
     */
    fun loadImage2(
        fragment: Fragment,
        imageView: ImageView,
        imgUrl: String
    ) {
        GlideApp.with(fragment).asDrawable().centerCrop().load(imgUrl).into(imageView)
    }

    /**
     * 加载图片
     */
    fun loadImage(
        context: Context,
        imageView: ImageView,
        imgUrl: String,
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(context).asDrawable().centerCrop().load(imgUrl)
            .placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 加载动图gif
     */
    fun loadGif(
        activity: Activity,
        imageView: ImageView,
        imgUrl: String,
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(activity).asGif().centerCrop().load(imgUrl)
            .placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 加载动图gif
     */
    fun loadGif(
        fragment: Fragment,
        imageView: ImageView,
        imgUrl: String,
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(fragment).asGif().centerCrop().load(imgUrl)
            .placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 加载圆形图片
     */
    fun loadCircleImage(
        activity: Activity,
        imageView: ImageView,
        imgUrl: String,
        borderSize: Int = 0,
        borderColor: Int = Color.TRANSPARENT,
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(activity).asDrawable().transform(
            CropCircleWithBorderTransformation(
                borderSize,
                borderColor
            )
        ).load(imgUrl).placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 加载圆形图片
     */
    fun loadCircleImage(
        fragment: Fragment,
        imageView: ImageView,
        imgUrl: String,
        borderSize: Int = 0,
        borderColor: Int = Color.TRANSPARENT,
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(fragment).asDrawable().transform(
            CropCircleWithBorderTransformation(
                borderSize,
                borderColor
            )
        ).load(imgUrl).placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 加载圆形图片
     */
    fun loadCircleImage(
        context: Context,
        imageView: ImageView,
        imgUrl: String,
        borderSize: Int = 0,
        borderColor: Int = Color.TRANSPARENT,
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(context).asDrawable().transform(
            CropCircleWithBorderTransformation(
                borderSize,
                borderColor
            )
        ).load(imgUrl).placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 加载圆角图片
     */
    fun loadRoundCornerImage(
        activity: Activity,
        imageView: ImageView,
        imgUrl: String,
        radius: Int = ScreenUtil.dip(BaseApplication.INSTANCE, 5),
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(activity).asDrawable().transform(
            MultiTransformation(
                CenterCrop(), RoundedCornersTransformation(
                    radius, 0
                )
            )
        ).load(imgUrl).placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 加载圆角图片
     */
    fun loadRoundCornerImage(
        fragment: Fragment,
        imageView: ImageView,
        imgUrl: String,
        radius: Int = ScreenUtil.dip(BaseApplication.INSTANCE, 5),
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(fragment).asDrawable().transform(
            MultiTransformation(
                CenterCrop(), RoundedCornersTransformation(
                    radius, 0
                )
            )
        ).load(imgUrl).placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 加载圆角图片
     */
    fun loadRoundCornerImage(
        context: Context,
        imageView: ImageView,
        imgUrl: String,
        radius: Int = ScreenUtil.dip(BaseApplication.INSTANCE, 5),
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(context).asDrawable().transform(
            MultiTransformation(
                CenterCrop(), RoundedCornersTransformation(
                    radius, 0
                )
            )
        ).load(imgUrl).placeholder(placeholder).error(error).into(imageView)
    }

    /**
     *
     */
    fun loadImageOrigin(
        context: Context,
        imageView: ImageView,
        imgUrl: String,
        placeholder: Int = placeholderResId,
        error: Int = errorResId
    ) {
        GlideApp.with(context).asDrawable().load(imgUrl)
            .placeholder(placeholder).error(error).into(imageView)
    }

    /**
     * 获取缓存中的图片
     */
    fun getCacheImage(context: Context, imageUrl: String): File =
        GlideApp.with(context).downloadOnly().load(imageUrl).submit().get()

    fun getCacheBitmap(context: Context, imageUrl: String): Bitmap =
        GlideApp.with(context).asBitmap().load(imageUrl).submit().get()

    fun setDefaultPlaceholder(resId: Int) {
        placeholderResId = resId
    }

    fun setDefaultError(resId: Int) {
        errorResId = resId
    }
}